package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.service.TeacherAuditFormService;
import com.cslg.questionnaires.utils.JsonMsgBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class TeacherAuditFormController {
    @Autowired
    private TeacherAuditFormService teacherAuditFormService;

    @GetMapping("/TeacherAuditForm")
    public ModelAndView getAuditFormPage(@RequestParam("page") int page, HttpSession session){
        String userType = (String) session.getAttribute("userType");
        if (userType.equals("Teacher")){
            Teacher teacher = (Teacher) session.getAttribute("user");
            String username = teacher.getUsername();
            String account = teacher.getAccount();
            HashMap<String, Object> msg = teacherAuditFormService.getUnAuditForm();
            msg.put("username",username);
            msg.put("account",account);
            msg.put("userType",userType);
            return new ModelAndView("TeacherAuditForm",msg);
        }else {
            return new ModelAndView("redirect:/"+userType+"Home?page=1");
        }
    }

    @PostMapping("/TeacherAuditForm/pass/{questionId}")
    @ResponseBody
    public HashMap<String,Object> passAuditForm(@PathVariable("questionId") int questionId,HttpSession session){
        String userType = (String)session.getAttribute("userType");
        if (userType.equals("Teacher")){
            return teacherAuditFormService.passAuditByQuestionId(questionId);
        }else {
            return JsonMsgBuilder.jsonBuild(401,"error");
        }
    }

    @PostMapping("/TeacherAuditForm/reject/{questionId}")
    @ResponseBody
    public  HashMap<String,Object> rejectAuditForm(@PathVariable("questionId") int questionId,HttpSession session){
        String userType = (String)session.getAttribute("userType");
        if (userType.equals("Teacher")){
            return teacherAuditFormService.rejectAuditByQuestionId(questionId);
        }else {
            return JsonMsgBuilder.jsonBuild(401,"error");
        }
    }

}
